package com.copa.l933;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author copa
 * @createDate 2022-06-09 21:17
 * @function 队列法
 */
public class Test1 {

    // 初版使用
    //private int counter;

    private Queue<Integer> queue;

    public Test1() {
        queue = new LinkedList<>();
    }

    public int ping(int t) {
//        counter = 0;
        queue.add(t);

//        for (Integer num : queue) {
//            // 由于我一开始看题有点懵逼，所以初版写成这样，虽然能通过...但是...你懂的o(╥﹏╥)o
//            if (num >= t - 3000 && num <= t) {
//                counter++;
//            }
//        }

        // 然后仔细看一下题目，发现给来的t是依次递增，所以区间也是依次递增的，于是写下优化版本...
        while (!queue.isEmpty()) {
            if (queue.peek() < t - 3000) {
                queue.poll();
                continue;
            }
            break;
        }
        return queue.size();
//        return counter;
    }

    public static void main(String[] args) {
        Test1 recentCounter = new Test1();
        // requests = [1]，范围是 [-2999,1]，返回 1
        System.out.println(recentCounter.ping(1));
        // requests = [1, 100]，范围是 [-2900,100]，返回 2
        System.out.println(recentCounter.ping(100));
        // requests = [1, 100, 3001]，范围是 [1,3001]，返回 3
        System.out.println(recentCounter.ping(3001));
        // requests = [1, 100, 3001, 3002]，范围是 [2,3002]，返回 3
        System.out.println(recentCounter.ping(3002));

    }
}
/**
 * Your RecentCounter object will be instantiated and called as such:
 * RecentCounter obj = new RecentCounter();
 * int param_1 = obj.ping(t);
 */